import * as mobx from 'mobx';
import { observable, action, computed, asMap } from 'mobx';
import * as Util from '../util/formUtil';
import * as FieldUtil from '../util/public/fieldUtil';
import * as _ from '../util/public/common';
import * as AI from '../util/public/analyzeData';

export default class Field{
    fieldMark = "";
    @observable value = "";
    @observable specialobj = null;
    @computed get numValue(){   //字段值转换为数值类型
        let _value = this.value.replace(/,/g, "");
        return FieldUtil.judgeIsNumber(_value) ? parseFloat(_value) : 0;;
    }
    @computed get valueObj(){
        return {value:this.value, specialobj:this.specialobj};
    }
    @observable variable = observable.map ? observable.map({}) : asMap({});     //字段可变区(viewattr、optionRange、customBrowserProp等)

    constructor(fieldMark="", valueObj={}, stepid){
        this.fieldMark = fieldMark;
        this.changeValueObj(valueObj);
        AI.appendStepDetail(stepid, this.fieldMark, AI.CHANGE_FIELD_VALUE, valueObj);
    }

    @action
    changeValueObj = (valueObj={}, stepid) =>{
        this.value = Util.convertNull(valueObj.value);
        this.specialobj = valueObj.specialobj || null;
        AI.appendStepDetail(stepid, this.fieldMark, AI.CHANGE_FIELD_VALUE, valueObj);
    }

    @action
    changeVariable = (obj={}, stepid) =>{
        !_.isEmpty(obj) && _.forIn(obj, (value,key) =>{
            this.variable.set(key, value);
        });
        AI.appendStepDetail(stepid, this.fieldMark, AI.CHANGE_FIELD_VARS, obj);
    }
    
}